# Install from CRAN
install.packages("tidyverse")
# Or the development version from GitHub
# install.packages("devtools")
devtools::install_github("tidyverse/tidyverse")
# or just ggplot2
install.packages("ggplot2")You can load ggplot2 with tidyverse or independently into your R session.
For more info, you can type into your R session
help(“ggplot2”)
Prices of over 50,000 round cut diamonds.
## # A tibble: 6 x 10
## carat cut color clarity depth table price x y z
## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
## 4 0.290 Premium I VS2 62.4 58 334 4.2 4.23 2.63
## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
## [1] 53940 10
price : price in US dollars ($326–$18,823)
carat : weight of the diamond (0.2–5.01)
data : Default dataset to use for plot (diamonds)
mapping : Default list of aesthetic mappings (visual properties) to use for plot (x= carat, y= price). If not specified, must be supplied in each layer added to the plot
A layer combines data, aesthetic mapping, geom (geometric object), stat (statistical transformation), and a position adjustment. Rendering a layer, you decide the type of ggplot2 object to create. However, the layers you can apply will depend on your data and geom object you select.
Layers can also combine different datasets into one unique ggplot2 object. This feature allows you to create sophisticated plots.
Learn more about these options by typing in your R session.
vignette(“ggplot2-specs”)
Consider the differential settings between geoms and stat elements
stat_density(
mapping = NULL,
data = NULL,
geom = “area”,
position = “stack”,
…)
geom_density(
mapping = NULL,
data = NULL,
stat = “density”,
position = “identity”,
…)
Coordinate systems
facet_wrap()
facet_grid()
theme()
color scales
ggplot(data=diamonds,mapping=aes(x=carat,y=price,colour=color)) + geom_point() + geom_smooth() +
facet_wrap(vars(color))ggplot(data=diamonds,mapping=aes(x=carat,y=price,colour=color)) + geom_point() + geom_smooth() +
facet_wrap(vars(color, clarity))ggplot(data=diamonds,mapping=aes(x=carat,y=price,colour=color)) + geom_point() +
facet_grid(cut ~ clarity)ggplot(data=diamonds,mapping=aes(x=carat,y=price,colour=color)) + geom_point() +
facet_grid(rows = vars(cut))ggplot(data=diamonds,mapping=aes(x=carat,y=price,colour=color)) + geom_point() +
facet_grid(cols = vars(clarity))ggplot(data=diamonds,mapping=aes(x=carat,y=price,colour=color)) + geom_point() +
facet_grid(vars(cut), vars(clarity))You can also manually modify components of a theme. For more info look at theme()
“Less is more!”
― Ludwig Mies Van Der Rohe.
(don’t overplot)
ggplot(data=diamonds,mapping=aes(x=carat,y=price,colour=color,shape=cut,linetype=color)) +
geom_point() + geom_line() + geom_smooth() + geom_rug()Hands-On Programming with R
R for Data Science
Data Science at the Command Line
This is an R Markdown presentation. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com